﻿using System.Web.Routing;
using Orchard.Environment;
using Orchard.Localization;
using Orchard.UI.Navigation;

namespace DreamStore.WebShop
{
    public class AdminMenu : INavigationProvider
    {
        private readonly Work<RequestContext> _requestContextAccessor; 
        private Localizer T { get; set; }

        public string MenuName
        {
            get { return "admin"; }
        }

        public AdminMenu(Work<RequestContext> requestContextAccessor)
        {
            _requestContextAccessor = requestContextAccessor;
            T = NullLocalizer.Instance;
        }

        public void GetNavigation(NavigationBuilder builder)
        {
            var requestContext = _requestContextAccessor.Value;
            var idValue = (string)requestContext.RouteData.Values["id"];
            var id = 0;
            if (!string.IsNullOrEmpty(idValue))
            {
                int.TryParse(idValue, out id);
            }

            builder
                .AddImageSet("webshop")

                .Add(item => item
                                 .Caption(T("Webshop"))
                                 .Position("2")
                                 .LinkToFirstChild(true)
                                 
                                 .Add(subItem => subItem
                                                    .Caption(T("Customers"))
                                                    .Position("2.1")
                                                    .Action("Index", "CustomerAdmin", new {area = "DreamStore.WebShop"})
                                                    .Add(T("Details"), i => i.Action("Edit", "CustomerAdmin", new { id }).LocalNav())
                                                    .Add(T("Addresses"), i => i.Action("ListAddresses", "CustomerAdmin", new { id }).LocalNav())
                                                    .Add(T("Orders"), i => i.Action("ListOrders", "CustomerAdmin", new { id }).LocalNav())
                                 )

                                 .Add(subItem => subItem
                                                    .Caption(T("All Orders"))
                                                    .Position("2.2")
                                                    .Action("Index", "OrderAdmin", new {area = "DreamStore.WebShop"})
                                 )

                                 .Add(subItem => subItem
                                                    .Caption(T("Customer Discounts"))
                                                    .Position("2.3")
                                                    .Action("Index", "CustomerDiscountAdmin", new {area = "DreamStore.WebShop"}))
                );
        }
    }
}